import { createLogger } from "../core/utils/logging";
import { SaveGameItemData } from "./savegame/savegame_item";
import { SaveGamePlayerData } from "./savegame/savegame_player";
import { SaveGameSundryData } from "./savegame/savegame_sundry";
import { SaveGameWorldData } from "./savegame/savegame_world";

const logger = createLogger('savegame');

export class SaveGame{
  constructor(app){
    this.app = app;

    this.savegameItemData = new SaveGameItemData(app);
    this.savegameWorldData = new SaveGameWorldData(app);
    this.savegamePlayerData = new SaveGamePlayerData(app);
    this.savegameSundryData = new SaveGameSundryData(app);
  }

  initialize(){
    return Promise.all([
      this.savegameItemData.initialize(),
      this.savegameWorldData.initialize(),
      this.savegamePlayerData.initialize(),
      this.savegameSundryData.initialize(),
    ])
  }

  saveData(data){
    let storeArr = [];
    if(data.playerData){
      storeArr.push(this.savegamePlayerData.serializeToWrite(data.playerData));
    }
    if(data.worldData){
      storeArr.push(this.savegameWorldData.serializeToWrite(data.worldData));
    }
    if(data.itemData){
      storeArr.push(this.savegameItemData.serializeToWrite(data.itemData));
    }
    if(data.sundryData){
      storeArr.push(this.savegameSundryData.serializeToWrite(data.sundryData));
    }

    return Promise.all(storeArr);
  }

  deleteAllData(){
    return Promise.all([
      this.savegamePlayerData.deleteAsync(),
      this.savegameItemData.deleteAsync(),
      this.savegameWorldData.deleteAsync(),
      this.savegameSundryData.deleteAsync(),
    ])
  }

  getPlayerData(){
    return this.savegamePlayerData.getData();
  }

  getWorldData(){
    return this.savegameWorldData.getData();
  }

  getItemData(){
    return this.savegameItemData.getData();
  }

  getSundryData(){
    return this.savegameSundryData.getData();
  }
}